// 配置数据库信息
const usersql = require('../../mysql.js')

// 导入bcryptjs密码加密模块
const bcrypt = require('bcryptjs')

// 获取用户列表
exports.getUser = (req, res) => {
    const pagemsg = req.query
    let sql = 'SELECT * FROM users,photos WHERE studentname=objid'
    if (req.query.studentname) sql = sql + " and studentname like '%" + req.query.studentname + "%'"
    if (req.query.studentid) sql = sql + " and studentid like '%" + req.query.studentid + "%'"
    if (req.query.classname) sql = sql + " and classname like '%" + req.query.classname + "%'"
    if (req.query.college) sql = sql + " and college like '%" + req.query.college + "%'"
    usersql.query(sql,(err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 0) {
            var p = pagemsg.pagesize*pagemsg.page
            var arr = results.slice(p - pagemsg.pagesize, p)
            res.send({ status: 200, message: '获取用户信息成功', data: { rows: arr, total: results.length } })
        } else {
            res.send({ status: 200, message: '找不到相应用户', data: { rows: [], total: 0 } })
        }
    })
}

// 获取单个用户详情信息
exports.getUserDetail = (req, res) => {
    const studentid = req.query.studentid
    const sql = 'select * from users where studentid=?'
    usersql.query(sql, studentid, (err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 0) {
            res.send({ status: 200, message: '获取用户信息成功', data: results[0] })
        } else {
            return res.cc('获取用户信息失败')
        }
    })
}

// 获取登录用户详情信息
exports.getLoginUserDetail = (req, res) => {
    const studentid = req.user.studentid
    const sql = 'select * from users where studentid=?'
    usersql.query(sql, studentid, (err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 0) {
            var user=results[0]
            user.roles={menu:[],points:[]}
            const sql2 = 'select distinct codes,type from user_role,role_right,rights where user_role.user_id=? and user_role.role_id =role_right.role_ids and role_right.right_id=rights.id'
            usersql.query(sql2, studentid, (err, results)=>{
                if (err) return res.cc(err)
                if (results.length !== 0){
                    results.forEach(val=>{
                        if(val.type==2){
                            user.roles.menu.push('/'+val.codes)
                        }else if(val.type==3){
                            user.roles.points.push(val.codes)
                        }
                    })
                    res.send({ status: 200, message: '获取用户信息成功', data: user })
                }else{
                    res.send({ status: 200, message: '获取用户无权限', data: user })
                }
            })
        } else {
            return res.cc('获取用户信息失败')
        }
    })
}

// 删除用户
exports.deleteUser = (req, res) => {
    const studentid = req.query.studentid
    console.log(req.query)
    const sql = 'delete from users where studentid=? '
    usersql.query(sql, studentid, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 0) {
            res.send({ status: 200, message: '删除用户成功', data: studentid })
        } else {
            return res.cc('删除用户失败！')
        }
    })
}
// 编辑用户信息
exports.updateUser = (req, res) => {
    const studentid = req.query.studentid
    // 需要提交整个用户信息
    const user = req.body
    if (user.password) user.password = bcrypt.hashSync(user.password, 10)
    const sql = 'update users set ? where studentid=? '
    usersql.query(sql, [user, studentid], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 0) {
            res.send({ status: 200, message: '更新用户角色成功', data: user })
        } else {
            return res.cc('更新用户角色失败！')
        }
    })
}
// 获取用户角色
exports.getUserRole = (req, res) => {
    // body包含角色role_id用户user_id
    const user_id = req.query.user_id
    const sql = 'select * from user_role,role where role_id=roleid and user_id=?'
    usersql.query(sql, user_id, (err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 0) {
            res.send({ status: 200, message: '获取用户角色成功', data: results })
        } else {
            res.send({ status: 200, message: '用户无角色', data: [] })
        }
    })
}